package br.com.ldml2;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import br.com.tmp.Temp;
import br.com.util.Escritor;
import br.com.util.Leitor;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.MapperWrapper;

public class Main {
	public static void main(String[] args) {
	
		XStream xs = new XStream() {
			protected MapperWrapper wrapMapper(MapperWrapper next) {  
				return new MapperWrapper(next) {  
					public boolean shouldSerializeMember(@SuppressWarnings("rawtypes") Class definedIn, String fieldName) {  
						return definedIn != Object.class ? super.shouldSerializeMember(definedIn, fieldName) : false;  
					}  
				};  
			}  
		};
		
		xs.processAnnotations(new Class[] { Mashup.class, Retrieve.class, Fusion.class, Temp.class } );
		
		String baseXml = "C:/Augusto/Programas/workspace/emml/WebContent/WEB-INF/classes/tmp.xml";
		
		Mashup m = (Mashup) xs.fromXML(new File(baseXml));
		
		Leitor l = new Leitor();
		l.abreArquivo(baseXml, false);
		String content = "";
		for(String s : l.leArquivoInteiro()) content += s + "\n";
		l.fechaArquivo();
		
		System.out.println("------------------ 'tmp.xml' Content -----------------");
		System.out.println(content);
		System.out.println("------------------ End of 'tmp.xml' Content -----------------");
		
		for(LdmlCommand lm : m.getCommandList()) {
			if(lm instanceof Fusion) System.out.println("Fusion convertido.");
			
			String fileName = "C:/Augusto/Programas/workspace/emml/WebContent/WEB-INF/classes/" + lm.getOutput() + ".xml";
			
			lm.exec(fileName);
			
			StringBuilder sb = new StringBuilder();
			sb.append("<variables>\n")
			  .append("\t\t<variable name=\"" + lm.getOutput() + "\" type=\"document\" />\n")
			  .append("\t</variables>\n")
			  .append("\t<macro:XMLConstructorFromFile filename=\"./" + lm.getOutput() + ".xml\" outputvariable=\"\\$result\" />");
					
			content = content.replaceFirst("<ldmlCommand .*/>", sb.toString());
			System.out.println(content);
		}
		
		String modifiedXml = "C:/Augusto/Programas/workspace/emml/WebContent/WEB-INF/classes/filters4.emml";
		
		Escritor e = new Escritor();
		e.abreArquivo(modifiedXml, false);
		e.gravaArquivo(new String[] { content });
		e.fechaArquivo();

//		try {
//			URL emml = new URL("http://localhost:8081/emml/filters2.emml");
//			URLConnection yc = emml.openConnection();
//	        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
//	        String inputLine;
//	        while ((inputLine = in.readLine()) != null) 
//	            System.out.println(inputLine);
//	        in.close();
//		} catch (MalformedURLException e1) {
//			e1.printStackTrace();
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}        
		
//		Mashup m = new Mashup();
//		
//		Retrieve r1 = new Retrieve();
//		r1.setEndpoint("http://dbpedia.org/sparql");
//		r1.setUri("http://dbpedia.org/resource/Brazil");
//		
//		LinkedList<LdmlCommand> list = new LinkedList<LdmlCommand>();
//		list.add(r1);
//		
//		m.setCommandList(list);
//		
//		System.out.println(xs.toXML(m));
//		m = (Mashup) xs.fromXML(xs.toXML(m));
//		for(LdmlCommand lc : m.getCommandList()) {
//			lc.exec();
//		}
	}
}
